package com.example.connect;

import android.content.Context;
import android.os.AsyncTask;


public class AsyncTask_Contact extends AsyncTask<Long, Void, Object> {

	static int m_processCount = 0;

	private int searchType = 0;

	private Context m_context = null;

	private Object m_apiData = null;

	AsyncTask_ApiListener m_interface = null;

	private ScreenStopper stopperDlg = null;


	
	void init(Context context , AsyncTask_ApiListener _interface,
			int type, Object apidata, int alertStyle)
	{
		if (m_processCount == 0)
		{
			m_context = context;
			m_interface =_interface;
			searchType = type;
			m_apiData = apidata;

			//アラートの見た目生成
			switch (alertStyle)
			{
				case ScreenStopper.STYLE_PROGRESS_PROCESS:
					stopperDlg = ScreenStopper.createScreenStopperProcess(m_context);
					break;
				case ScreenStopper.STYLE_PROGRESS_VOTE:
					stopperDlg = ScreenStopper.createScreenStopperVote(m_context);
					break;
				case ScreenStopper.STYLE_PROGRESS_DRAFT:
					stopperDlg = ScreenStopper.createScreenStopperDraft(m_context);
					break;
				case ScreenStopper.STYLE_PROGRESS_LOADING:
					stopperDlg = ScreenStopper.createScreenStopperLoading(m_context);
					break;
				default:
					stopperDlg = ScreenStopper.createScreenStopperTrans(m_context);
					break;
			}
			stopperDlg.show();
		}

		m_processCount++;
	}


	
	public AsyncTask_Contact(Context context , AsyncTask_ApiListener _interface, int type, Object apidata)
	{
		init(context , _interface,
				type, apidata, ScreenStopper.STYLE_STOPPER);
	}

	
	public AsyncTask_Contact(Context context , AsyncTask_ApiListener _interface,
			int type, Object apidata, int alertStyle)
	{
		init(context , _interface,
				type, apidata, alertStyle);
	}


	@Override
	protected Object doInBackground(Long... params) {
		if (m_interface == null)
		{
			return null;
		}
		return m_interface.callApi(searchType, m_apiData);
	}

	@Override
	protected void onPostExecute(Object apiresult)
	{
		if (m_interface != null)
		{
			stopperDlg.dismiss();
			stopperDlg = null;

			m_interface.finishApi(searchType, apiresult);

		}

		m_processCount --;

		super.onPostExecute(apiresult);
	}

}
